/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.senaclem.persistencia;

import co.edu.senaclem.modelo.Fichatitulacion;
import co.edu.senaclem.modelo.Instructor;
import co.edu.senaclem.modelo.Lideresficha;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DILOVE
 */
@Stateless
public class LideresDAO implements ILideresDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void registrar(Lideresficha lideresFicha) throws Exception {
        try {
            entityManager.persist(lideresFicha);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void eliminar(Lideresficha lideresFicha) throws Exception {
        try {
            entityManager.remove(lideresFicha);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Lideresficha> consultarPorInstructor(Integer documentoInstructor) throws Exception {
        try {
            String jpql = "Select l From Lideresficha l where l.documentoinstructor=" + documentoInstructor + "";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public Lideresficha consultarPorFicha(Integer fichatitulacion) throws Exception {
        try {
            String jpql = "Select l From Lideresficha l where l.fichatitulacion.fichatitulacion=" + fichatitulacion + " and l.fechaasignacion IN(select max(f.fechaasignacion) from Lideresficha f);";
            Query query = entityManager.createQuery(jpql);
            return (Lideresficha) query.getSingleResult();
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Lideresficha> consultar() throws Exception {
        try {
            String jpql="Select c From Lideresficha c";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }

}
